package com.tongzhou.tongzhou.link.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.link.dao.ILinkDAO;
import com.tongzhou.tongzhou.link.model.Degree;
import com.tongzhou.tongzhou.link.model.Link;
import com.tongzhou.tongzhou.link.model.SearchLinkCondition;
import com.tongzhou.util.Util;

public class LinkDAO implements ILinkDAO{

	@Override
	public List<Degree> getDegreeList(int link_id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.degree_name,p.start_time_up,p.start_time_down,p.end_time_up,p.end_time_down,p.link_id ");
		sql.append(" from degree p ");
		sql.append(" where 1=1 ");
		sql.append(" and p.link_id="+link_id+" ");
		List<Degree> result=DBUtil.query(sql.toString(),new BeanListHandler<Degree>(Degree.class));
		return result;
	}

	@Override
	public List<Link> getLinktByProId(int proid) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,link_name,productSchedule_id ");
		sql.append(" from link ");
		sql.append(" where 1=1 ");
		sql.append(" and productSchedule_id='"+proid+"'");
		List<Link> result=DBUtil.query(sql.toString(),new BeanListHandler<Link>(Link.class));
		return result;
	}

	@Override
	public int addLink(Link link,String itemsVal) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.delete(0, sql.length());
		sql.append("select max(id)+1 from link");
		Long maxLinkId=Long.valueOf(1);
		try{
			maxLinkId=DBUtil.getCount(sql.toString());
		}catch (DBException e) {
			e.printStackTrace();
		}
		if(maxLinkId==null || maxLinkId.equals("")){
			maxLinkId=Long.valueOf(1);
		}
		sql.append("insert into link(id,link_name,productSchedule_id) ");
		sql.append("values('"+maxLinkId+"',");
		sql.append("'"+Util.changeToObj(link.getLink_name())+"',");
		sql.append("'"+link.getProductSchedule_id()+"')");
		int result=DBUtil.update(sql.toString());
		String[] items=itemsVal.split("※");
		for(int i=0;i<items.length;i++){
			String[] item=items[i].split("`");
			sql.delete(0, sql.length());
			sql.append("insert into degree(degree_name,start_time_up,start_time_down,end_time_up,end_time_down,link_id) ");
			sql.append("values('"+Util.changeToObj(item[0])+"',");
			sql.append("'"+Util.changeToObj(item[1])+"',");
			sql.append("'"+Util.changeToObj(item[2])+"',");
			sql.append("'"+Util.changeToObj(item[3])+"',");
			sql.append("'"+Util.changeToObj(item[4])+"',");
			sql.append("'"+maxLinkId+"')");
			DBUtil.update(sql.toString());
		}
		return result;
	}

	@Override
	public void deleteLink(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from link where id='"+id+"'");
		DBUtil.update(sql.toString());
		
		sql.delete(0, sql.length());
		sql.append("delete from degree where link_id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public Link getLinkById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,link_name,productSchedule_id ");
		sql.append(" from link ");
		sql.append(" where id='"+id+"'");
		Link link=DBUtil.query(sql.toString(), new BeanHandler<Link>(Link.class));
		return link;
	}

	@Override
	public List<Link> listLink(SearchLinkCondition condition)
			throws DBException {
		List<Link> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from link ");
		sql.append(" where 1=1 ");
		sql.append(" and productSchedule_id='"+condition.getProductSchedule_id()+"'");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,link_name,productSchedule_id ");
		sql.append(" from link ");
		sql.append(" where 1=1 ");
		sql.append(" and productSchedule_id='"+condition.getProductSchedule_id()+"'");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<Link>(Link.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateLink(Link link,String itemsVal) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update link set ");
		sql.append("link_name='"+Util.changeToObj(link.getLink_name())+"' ");
		sql.append("where id='"+link.getId()+"'");
		DBUtil.update(sql.toString());
		
		sql.delete(0, sql.length());
		sql.append("delete from degree where link_id='"+link.getId()+"'");
		DBUtil.update(sql.toString());
		
		String[] items=itemsVal.split("※");
		for(int i=0;i<items.length;i++){
			String[] item=items[i].split("`");
			sql.delete(0, sql.length());
			sql.append("insert into degree(degree_name,start_time_up,start_time_down,end_time_up,end_time_down,link_id) ");
			sql.append("values('"+Util.changeToObj(item[0])+"',");
			sql.append("'"+Util.changeToObj(item[1])+"',");
			sql.append("'"+Util.changeToObj(item[2])+"',");
			sql.append("'"+Util.changeToObj(item[3])+"',");
			sql.append("'"+Util.changeToObj(item[4])+"',");
			sql.append("'"+link.getId()+"')");
			DBUtil.update(sql.toString());
		}
	}

	@Override
	public List<Degree> getDegreeByLinkId(int linkId) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.degree_name,p.start_time_up,p.start_time_down,p.end_time_up,p.end_time_down,p.link_id ");
		sql.append(" from degree p ");
		sql.append(" where 1=1 ");
		sql.append(" and p.link_id='"+linkId+"' ");
		sql.append(" order by p.start_time_up,p.start_time_down asc ");
		List<Degree> result=DBUtil.query(sql.toString(),new BeanListHandler<Degree>(Degree.class));
		return result;
	}

}
